package com.pengshun.modules.api.user.service.impl;

import com.pengshun.modules.api.mapper.PsUserInfoMapper;
import com.pengshun.modules.api.user.para.CarPara;
import com.pengshun.modules.api.user.service.UserCarBossService;
import com.pengshun.modules.api.user.service.dto.CarOwnerDto;
import com.pengshun.modules.api.user.service.dto.CarUser;
import com.pengshun.modules.api.user.service.dto.UserDriverDto;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.common.CarDrivingStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserCarBossServiceImpl implements UserCarBossService {

    private final PsUserInfoMapper userInfoMapper;

    @Override
    public BaseResult list(CarPara para) {
        int page = (para.getPage()) * para.getSize();
        List<UserDriverDto> list = userInfoMapper.selectUserBossList(para.getUserId(), para.getStatus(), page < 0 ? 0 : page, para.getSize());
        for (UserDriverDto dto : list) {
            if (dto.getStatus().longValue() == CarDrivingStatus.TO_ASSOCIATED) {
                //带关联
                if (para.getUserId().equals(dto.getDrivingUserId())&&dto.getType()==2) {
                    dto.setShowContent("待自己同意");
                    dto.setIsShow(1);
                }
                if (para.getUserId().equals(dto.getDrivingUserId())&&dto.getType()==1) {
                    dto.setShowContent("等待对方同意");
                    dto.setIsShow(0);
                }
            }
            if (dto.getStatus().longValue() == CarDrivingStatus.TO_SOLUTION) {
                //带关联
                if (para.getUserId().equals(dto.getDrivingUserId())&&dto.getType()==2) {
                    dto.setShowContent("自己拒绝");
                    dto.setIsShow(1);
                }
                if (para.getUserId().equals(dto.getDrivingUserId())&&dto.getType()==1) {
                    dto.setShowContent("对方拒绝");
                    dto.setIsShow(0);
                }
            }
            if (dto.getStatus().longValue() == CarDrivingStatus.HAVE_UNBUNDLING) {
                //带关联
//                if (para.getUserId().equals(dto.getDrivingUserId())&&dto.getType()==2) {
                    dto.setShowContent("对方解绑");
//                }
//                if (para.getUserId().equals(dto.getDrivingUserId())&&dto.getType()==1) {
//                    dto.setShowContent("自己解绑");
//                }
            }
        }
        return new BaseResult(0, list);
    }

    @Override
    public BaseResult carList(Long id) {
        //
        List<CarOwnerDto> list = userInfoMapper.carList(id);
        List<UserDriverDto> lists = userInfoMapper.selectUserCarDriverList(id);
        List<CarUser> users = new ArrayList<>();
        CarUser carUser=new CarUser();
        carUser.setGatheringUserId(id);
        carUser.setGatheringUserName("自己");
        carUser.setDtos(list.size() > 0 ? list : new ArrayList<>());
        users.add(carUser);
        if (lists.size() > 0) {
            for (UserDriverDto dto : lists) {

                CarUser user=new CarUser();
                user.setGatheringUserId(dto.getCarUserId());
                user.setGatheringUserName( dto.getName());
                user.setGatheringUserPhone(dto.getPhone());
                user.setDtos(dto.getCarOwnerDtos().size() > 0 ? dto.getCarOwnerDtos() : new ArrayList<>());
                users.add(user);
            }
        }

        return new BaseResult(0, users);
    }
}
